package com.dzt.androidframework.network;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.TitleFrameActivity;
import com.dzt.androidframework.databinding.ActivityNetworkBinding;
import com.dzt.androidframework.network.mobapi.cook.CategoryActivity;
import com.dzt.androidframework.network.mobapi.lottery.LotteryActivity;
import com.dzt.androidframework.utils.JLogKit;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class NetworkActivity extends TitleFrameActivity<ActivityNetworkBinding> {

	private BookPresenter mBookPresenter;

	@Override
	protected void initData(Bundle savedInstanceState) {
		mBookPresenter = new BookPresenter(context);
		mBookPresenter.onCreate();
		mBookPresenter.attachView(mBookView);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_network;
	}

	@Override
	protected void initWidgets() {
		setTitle(getString(R.string.menu_12));
		bindingView.btnRequest.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mBookPresenter.getSearchBooks("西游记", null, 0, 3);
			}
		});

		bindingView.btnRequestLottery.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivity(LotteryActivity.class, null);
			}
		});

		bindingView.btnRequestCook.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				startActivity(CategoryActivity.class, null);
			}
		});

		Retrofit retrofit = new Retrofit.Builder()
				.baseUrl("https://www.baidu.com/")
				.build();
		RetrofitService service = retrofit.create(RetrofitService.class);
		Call<ResponseBody> call = service.getBaidu("value");
		call.enqueue(new Callback<ResponseBody>() {
			@Override
			public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
				try {
					JLogKit.getInstance().i("" + response.body().string());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<ResponseBody> call, Throwable t) {

			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mBookPresenter.onDestroy();
	}

	private BookView mBookView = new BookView() {
		@Override
		public void onSuccess(Book mBook) {
			bindingView.tvText.setText(mBook.toString());
		}

		@Override
		public void onError(String result) {
			Toast.makeText(NetworkActivity.this, result, Toast.LENGTH_SHORT).show();
		}
	};
}
